﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace System
{
    /// <summary>
    /// Json帮助类
    /// </summary>
    public class JsonHelper
    {
        static JsonHelper()
        {
            JsonConvert.DefaultSettings = GetSetting;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        private static JsonSerializerSettings GetSetting()
        {
            var result = new JsonSerializerSettings();
            result.Error = OnError;
            return result;
        }

        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs e)
        {
            e.ErrorContext.Handled = true;
        }

        /// <summary>
        /// 序列号json
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Serialize(object data)
        {
            return JsonConvert.SerializeObject(data);
        }

        public static async Task SerializeToFile(object jsonObject, string file)
        {
            string strJsonObj = Serialize(jsonObject);
            if (string.IsNullOrEmpty(strJsonObj))
                return;
            using (FileStream fs = new FileStream(file, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    await sw.WriteAsync(strJsonObj);
                }
            }
        }

        /// <summary>
        /// 反序列化json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeSerialize<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 反序列号json,并放进对象内
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="anonymouseTypObj"></param>
        /// <returns></returns>
        public static T DeSerialize<T>(string json, T anonymouseTypObj)
        {
            try
            {
                return JsonConvert.DeserializeAnonymousType(json, anonymouseTypObj);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static T DeserializeFromFile<T>(string file)
        {
            using (FileStream stream = new FileStream(file, FileMode.Open))
            {
                using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8))
                {
                    string text = streamReader.ReadToEnd();
                    if (!string.IsNullOrEmpty(text))
                    {
                        return DeSerialize<T>(text);
                    }
                }
            }
            return default(T);
        }
    }
}
